package com.heer.api.sign;

import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.jar.Pack200;

public class SignHelper {
    private HashMap<String, String> parameters = new HashMap<String, String>();
    private String AppKey = "75526093";
    private String AppSecret = "6w1uyx2io21f02srv2571x4myafpyldc";
    private static String Token;

    public SignHelper(String method) {
        parameters.put("key", AppKey);
        parameters.put("unixtime", String.valueOf(System.currentTimeMillis()));
        if (!TextUtils.isEmpty(Token))
            parameters.put("token", Token);
    }

    public String getToken()
    {
        return Token;
    }

    public String GetParameter(String key) {
        return getParameters().get(key);
    }

    public Map<String, String> GetParam() throws SDKRuntimeException {
        if ("" == AppSecret) {
            throw new SDKRuntimeException("密钥不能为空！");
        }
        String unSignParaString = CommonUtil.FormatBizQueryParaMap(getParameters(), false);
//		String paraString = CommonUtil.FormatBizQueryParaMap(getParameters(), true);
//		return paraString + "&sign=" + MD5SignUtil.Sign(unSignParaString, AppSecret);
        parameters.put("sign", MD5SignUtil.Sign(unSignParaString, AppSecret));
        return parameters;
    }


    public HashMap<String, String> getParameters() {
        return parameters;
    }


    public void setParameters(HashMap<String, String> parameters) {
        this.parameters = parameters;
    }

    public void SetParameter(String key, String value) {
        this.parameters.put(key, value);
    }

    public String getSign() throws SDKRuntimeException {
        return MD5SignUtil.Sign(CommonUtil.FormatBizQueryParaMap(getParameters(), false), AppSecret);
    }

    public static void initToken(String token) {
        if (!TextUtils.isEmpty(token))
            Token = token;
    }
}
